package com.tz.cartelo.activity;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tz.cartelo.BaseApplication;
import com.tz.cartelo.R;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.result.Result;
import com.tz.cartelo.util.AES;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;
import com.tz.common.util.VolleyDataUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * Created by Administrator on 2017/4/16.
 */

public class ChangePasswordActivity extends DataLoadActivity implements View.OnClickListener {
    private static final String TAG = "ChangePasswordActivity";
    private EditText mChangePasswordOldPassword, mChangePasswordNewPassword, mChangePasswordNewPasswordAgain;
    private ImageView mChangePasswordOldPasswordClean, mChangePasswordNewPasswordClean, mChangePasswordNewPasswordAgainClean, mChangePasswordShowPassword;
    private Api mApi;
    private boolean mIsShowPassword;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void init() {
        mChangePasswordOldPassword = (EditText) findViewById(R.id.change_password_old_password);
        mChangePasswordNewPassword = (EditText) findViewById(R.id.change_password_new_password);
        mChangePasswordNewPasswordAgain = (EditText) findViewById(R.id.change_password_new_password_again);
        mChangePasswordOldPasswordClean = (ImageView) findViewById(R.id.change_password_old_password_clean);
        mChangePasswordNewPasswordClean = (ImageView) findViewById(R.id.change_password_new_password_clean);
        mChangePasswordNewPasswordAgainClean = (ImageView) findViewById(R.id.change_password_new_password_again_clean);
        mChangePasswordShowPassword = (ImageView) findViewById(R.id.change_password_show_password_img);
        findViewById(R.id.change_password_show_password_layout).setOnClickListener(this);
        mChangePasswordNewPasswordClean.setOnClickListener(this);
        mChangePasswordNewPasswordAgainClean.setOnClickListener(this);
        findViewById(R.id.change_password_btn).setOnClickListener(this);
        findViewById(R.id.change_password_forget_password).setOnClickListener(this);

        mChangePasswordOldPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b)
                    mChangePasswordOldPasswordClean.setVisibility(View.VISIBLE);
                else
                    mChangePasswordOldPasswordClean.setVisibility(View.GONE);
            }
        });
        mChangePasswordNewPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b)
                    mChangePasswordNewPasswordClean.setVisibility(View.VISIBLE);
                else
                    mChangePasswordNewPasswordClean.setVisibility(View.GONE);
            }
        });
        mChangePasswordNewPasswordAgain.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b)
                    mChangePasswordNewPasswordAgainClean.setVisibility(View.VISIBLE);
                else
                    mChangePasswordNewPasswordAgainClean.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void initTitle() {
        ImageView titleBack = (ImageView) findViewById(R.id.title_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(this);

        TextView titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(R.string.change_password);
        TextView titleRightText = (TextView) findViewById(R.id.title_right_text);
        titleRightText.setVisibility(View.VISIBLE);
        titleRightText.setText("完成");
        titleRightText.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_right_text:
//                if (mChangePasswordOldPassword.getText().toString().trim().isEmpty()) {
//                    showToast(R.string.change_password_old_password_hint);
//                } else
                if (mChangePasswordNewPassword.getText().toString().trim().isEmpty()) {
                    showToast(R.string.change_password_new_password_hint);
                } else if (mChangePasswordNewPasswordAgain.getText().toString().trim().isEmpty()) {
                    showToast(R.string.change_password_new_password_again_hint);
                } else if (mChangePasswordNewPassword.getText().toString().trim().length() > 12) {
                    showToast(R.string.change_password_wrong);
                } else if (mChangePasswordNewPasswordAgain.getText().toString().trim().length() > 12) {
                    showToast(R.string.change_password_wrong);
                } else if (!mChangePasswordNewPassword.getText().toString().trim().equals(mChangePasswordNewPasswordAgain.getText().toString().trim())) {
                    showToast(R.string.change_password_dif);
                } else {
                    mApi = Api.CHANGE_LOGIN_PHONE;
                    loadData(mApi, true);
                }
                break;
            case R.id.change_password_old_password_clean:
                mChangePasswordOldPassword.setText("");
                break;
            case R.id.change_password_new_password_clean:
                mChangePasswordNewPassword.setText("");
                break;
            case R.id.change_password_new_password_again_clean:
                mChangePasswordNewPasswordAgain.setText("");
                break;
            case R.id.change_password_show_password_layout:
                if (mIsShowPassword) {
                    mChangePasswordNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mChangePasswordNewPasswordAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIsShowPassword = false;
                    mChangePasswordShowPassword.setImageResource(R.mipmap.ic_password_close);
                } else {
                    mChangePasswordNewPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mChangePasswordNewPasswordAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIsShowPassword = true;
                    mChangePasswordShowPassword.setImageResource(R.mipmap.ic_password_show);
                }
                break;
            case R.id.change_password_btn:
                break;
            case R.id.change_password_forget_password:
                switchActivity(SafeTestActivity.class, null);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case CHANGE_LOGIN_PHONE:
                VolleyDataUtils.mHeaders.put("Cookie", "JSESSIONID=" + PreferencesUtils.getString(Key.SESSION_ID, ""));

                params.addParam("password", mChangePasswordNewPassword.getText().toString().trim());
                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                String time = System.currentTimeMillis() + "";
                params.addParam("time", time);
                params.addParam("uniqueIdentifier", getIntent().getStringExtra("uniqueIdentifier"));
                List<String> dataList = new ArrayList<>();
                dataList.add(mChangePasswordNewPassword.getText().toString().trim());
                dataList.add(PreferencesUtils.getString(Key.PER_USER_ID, ""));
                dataList.add(time);
                dataList.add(getIntent().getStringExtra("uniqueIdentifier"));

                try {
                    params.addParam("sign", AES.createASE(dataList));
                } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case CHANGE_LOGIN_PHONE:
                Result result = fromJson(response, Result.class);
                if (!result.isSuccess()) {
                    showToast(result.getMessage());
                    return;
                }
                showToast(R.string.change_password_succeed);
                PreferencesUtils.clearPre();//清空本地保存数据
                BaseApplication.clearActivity();
                switchActivityAndFinish(LoginActivity.class, null);

                break;
            default:
                break;
        }
    }

}
